package tech.eyao.hs60x.token;

import tech.eyao.hs60x.common.CommonConstants;
import tech.eyao.hs60x.utils.JwtHelper;

/**
 * 抽象TokenManager
 * 
 * @author hejian
 *
 */
public abstract class AbstractTokenManager {
	

	/**
	 * 创建token
	 * 
	 * @param userId
	 * @return
	 */
	protected static String createToken(long userId) {
		/*Token token = new Token(Token.hash(userId));
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
			md5.update(token.toString().getBytes());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		int index;
		StringBuffer buf = new StringBuffer("");
		byte[] b = md5.digest();
		for (int offset = 0; offset < b.length; offset++) {
			index = b[offset];
			if (index < 0)
				index += 256;
			if (index < 16)
				buf.append("0");
			buf.append(Integer.toHexString(index));
		}
		return buf.toString();*/
		return "";
	}
	
	/**
	 *  创建token
	 * @param userId
	 * @param deviceId
	 * @return
	 */
	protected static String createToken(long userId,String deviceId) {
		return JwtHelper.createJWT(userId, deviceId, CommonConstants.EXPIRATION_DAY);
	}

	/**
	 * 验证失效token
	 */
	public abstract void verifyExpired();

	/**
	 * 用户授权成功后将授权信息存入
	 * 
	 * @param token
	 * @param loginUser
	 */
	public abstract void addToken(String token, Object loginUser);

	/**
	 * 验证令牌有效性,有效则延长session生命周期
	 * 
	 * @param token
	 * @return
	 */
	public abstract Object validate(String token);

	/**
	 * 移除令牌
	 * 
	 * @param token
	 */
	public abstract void remove(String token);
}
